import 'package:share_fit/app/controllers/base/base_controller.dart';
import 'package:share_fit/app/ute/uteped_const.dart';

import 'sedentary_const.dart';
import 'sedentary_remind_state.dart';

class SedentaryRemindLogic extends BaseController {
  final SedentaryRemindState state = SedentaryRemindState();

  void getActivityRemind() {
    dRemoteReqAsync(SedentaryConst.methodGetActivityRemind, {})
        .then((value) => _onGetActivityRemind(value));
  }

  void _onGetActivityRemind(Map map) {
    if (map[UTEPedConst.keySuccess]) {
      state.switchRemind = map[UTEPedConst.keyEnable];
      update(['sedentary_remind']);
    }
  }

  void switchRemind(bool value) {
    showLoading();
    state.switchRemind = value;
    bRemoteReqAsync(SedentaryConst.methodSetActivityRemind, {UTEPedConst.keyEnable: value, "remindInterval": 60})
        .then((success) => _onSetSedentaryRemindEnable(success, value));
    update(['sedentary_remind']);
  }

  void _onSetSedentaryRemindEnable(success, value) {
    cancelLoading();
    if (success) {
      save(SedentaryConst.methodSetActivityRemind, value);
    }
    else {
      state.switchRemind = !value;
    }
    update(['sedentary_remind']);
  }

  void switchDisturb(bool value) {
    state.switchDisturb = value;
    update(['sedentary_disturb']);
  }

  void setStartTime(hour, min) {
    state.startTimeHour = hour;
    state.startTimeMin = min;
    state.startTime = _getTime(hour, min);

    bRemoteReqAsync(SedentaryConst.methodSetActivityRemind, {SedentaryConst.keyFromTimeHour: hour, SedentaryConst.keyFromTimeMinute:min})
        .then((success) => {
      if (success) {
        save(SedentaryConst.keyFromTimeHourSedentary, hour), save(SedentaryConst.keyFromTimeMinuteSedentary, min)
      }
    });

    update(['sedentary_start_time']);
  }

  void setEndTime(hour, min) {
    state.endTimeHour = hour;
    state.endTimeMin = min;
    state.endTime = _getTime(hour, min);

    bRemoteReqAsync(SedentaryConst.methodSetActivityRemind, {SedentaryConst.keyToTimeHour: hour, SedentaryConst.keyToTimeMinute:min})
        .then((success) => {
      if (success) {
        save(SedentaryConst.keyToTimeHourSedentary, hour), save(SedentaryConst.keyToTimeMinuteSedentary, min)
      }
    });

    update(['sedentary_end_time']);
  }

  @override
  String getRemotePath() {
    return "sedentary";
  }

  @override
  String getStoragePath() {
    return UTEPedConst.pathDevice;
  }

  @override
  void initState() {
    state.is24HourFormat = read(UTEPedConst.keyTimeFormat, defaultValue: false);
    state.switchRemind = read(SedentaryConst.methodSetActivityRemind, defaultValue: false);

    state.startTimeHour = read(SedentaryConst.keyFromTimeHourSedentary, defaultValue: 8);
    state.startTimeMin = read(SedentaryConst.keyFromTimeMinuteSedentary, defaultValue: 0);
    state.startTime = _getTime(state.startTimeHour, state.startTimeMin);

    state.endTimeHour = read(SedentaryConst.keyToTimeHourSedentary, defaultValue: 9);
    state.endTimeMin = read(SedentaryConst.keyToTimeMinuteSedentary, defaultValue: 0);
    state.endTime = _getTime(state.endTimeHour, state.endTimeMin);

    getActivityRemind();
  }

  @override
  void onRemoteEvent(event, method) {
    // TODO: implement onRemoteEvent
  }

  String _getTime(hour, min) {
    var tempHour = hour;
    if (hour > 12 && !state.is24HourFormat) {
      tempHour -= 12;
    }

    var tempMin = min.toString();
    if (min == 0) {
      tempMin = "00";
    }

    return "$tempHour:$tempMin";
  }
}
